package cn.wit.job.service.impl;

import cn.wit.job.mapper.JobCategoryMapper;
import cn.wit.job.mapper.RecruitInfoMapper;
import cn.wit.utils.pojo.JobCategory;
import cn.wit.utils.pojo.RecruitInfo;
import cn.wit.job.service.JobCategoryService;
import cn.wit.job.service.RecruitInfoService;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class JobCategoryServiceImpl implements JobCategoryService {
    @Autowired
    private JobCategoryMapper jobCategoryMapper;
    @Autowired
    private RecruitInfoMapper recruitInfoMapper;

    @Autowired
    private RecruitInfoService recruitInfoService;

    @Override
    public Result insert(JobCategory job_category) {
        int i = jobCategoryMapper.insert(job_category);
        if(i > 0){
            return Result.ok();
        }
        return Result.error("新增失败");
    }

    @Override
    public Result deleteById(int id) {
        //如果RecruitInfo中没有依赖该Id的数据，那么可以删除，否则不给删除,根据外键查询
        QueryWrapper<RecruitInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("job_ID", id);
        List<RecruitInfo> recruitInfos = recruitInfoMapper.selectList(wrapper);
        if(CollectionUtils.isEmpty(recruitInfos)){
            int i = jobCategoryMapper.deleteById(id);
            if(i > 0){
                return Result.ok();
            }
        }
        return Result.error("删除失败");
    }

    @Override
    public Result updateById(JobCategory job_category) {
        int i = jobCategoryMapper.updateById(job_category);
        if(i > 0){
            return Result.ok();
        }
        return Result.error("更新失败");
    }

    @Override
    public Result selectAllJobCategory() {
        List<JobCategory> jobCategories = jobCategoryMapper.selectList(null);
        if(!CollectionUtils.isEmpty(jobCategories)){
            return Result.ok(jobCategories);
        }
        return Result.ok(new ArrayList<>());
    }

    @Override
    public Result selectAllPage(IPage<JobCategory> page) {
        IPage<JobCategory> jobCategoryPage = jobCategoryMapper.selectPage(page, null);
        if(jobCategoryPage.getRecords() != null && jobCategoryPage.getRecords().size() > 0){
            Result res = Result.ok(jobCategoryPage);
            return res;
        }
        return Result.error("未查询到");
    }

    @Override
    public Result selectById(Integer id) {
        return Result.ok(jobCategoryMapper.selectById(id));
    }
}
